package cc.angis.xiugallery.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import cc.angis.xiugallery.data.UserAccount;

public class UserAccountDBHelper extends BaseDBHelper {
	
	public UserAccountDBHelper(){
		
	}
	
	public UserAccountDBHelper(Context context, String table){
		super(context, table);
	}
	
	public UserAccountDBHelper(Context context, String table, ContentValues values){
		super(context, table, values);
	}
	
	public long delete(int id){
		mWhereClaus = "id=?";
		mWhereArgs = new String[] { String.valueOf(id) };
		return delDB();
	}
	
	public long deleteByType(int type){
		mWhereClaus = "accounttype=?";
		mWhereArgs = new String[] { String.valueOf(type) };
		return delDB();
	}
	
	public long delete(String uid){
		mWhereClaus = "uid=?";
		mWhereArgs = new String[] { uid };
		return delDB();
	}
	
	public long update(String uid, String username, String accessToken, long expiredIn, int type, String avatar){
		mWhereClaus = "uid=? AND accounttype=?";
		mWhereArgs = new String[] { uid, String.valueOf(type) };
		mValues = new ContentValues();
		if (username != null)
			mValues.put(TableConstants.UserAccount.USERNAME, username);
		if (username != null)
			mValues.put(TableConstants.UserAccount.ACCESS_TOKEN, accessToken);
		if (expiredIn != -1)
			mValues.put(TableConstants.UserAccount.EXPIRED_IN, expiredIn);
		if (avatar != null)
			mValues.put(TableConstants.UserAccount.AVATAR, avatar);
		return updateDB();
	}
	
	public long insert(String uid, String username, String accessToken, long expiredIn, int type, String avatar){
		mValues = new ContentValues();
		mValues.put(TableConstants.UserAccount.USER_ID, uid);
		mValues.put(TableConstants.UserAccount.USERNAME, username);
		mValues.put(TableConstants.UserAccount.ACCESS_TOKEN, accessToken);
		mValues.put(TableConstants.UserAccount.EXPIRED_IN, expiredIn);
		mValues.put(TableConstants.UserAccount.ACCOUNT_TYPE, type);
		mValues.put(TableConstants.UserAccount.AVATAR, avatar);
		return insertDB();
	}
	
	public List<UserAccount> getAllUserAccounts(){
		List<UserAccount> UserAccounts = new ArrayList<UserAccount>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, null, null, null, null, null, null);
		int idColumn = cursor.getColumnIndex(
				TableConstants.UserAccount.ID);
		int uidColumn = cursor.getColumnIndex(
				TableConstants.UserAccount.USER_ID);
		int usernameColumn = cursor.getColumnIndex(
				TableConstants.UserAccount.USERNAME);
		int tokenColumn = cursor.getColumnIndex(
				TableConstants.UserAccount.ACCESS_TOKEN);
		int expiredInColumn = cursor.getColumnIndex(
				TableConstants.UserAccount.EXPIRED_IN);
		int typeColumn = cursor.getColumnIndex(
				TableConstants.UserAccount.ACCOUNT_TYPE);
		int avatarColumn = cursor.getColumnIndex(TableConstants.UserAccount.AVATAR);
		if (cursor.moveToFirst()){
			UserAccount userInfo;
			do {
				userInfo = new UserAccount();
				userInfo.setId(cursor.getInt(idColumn));
				userInfo.setUid(cursor.getString(uidColumn));
				userInfo.setAccessToken(cursor.getString(tokenColumn));
				userInfo.setUsername(cursor.getString(usernameColumn));
				userInfo.setExpiredIn(cursor.getLong(expiredInColumn));
				userInfo.setType(cursor.getInt(typeColumn));
				userInfo.setAvatar(cursor.getString(avatarColumn));
				UserAccounts.add(userInfo);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return UserAccounts;
	}
	
}
